// pages/dingdan/dingdan.js
const {addressList,affirm,rentalEquipment,payment,xieyi,deladdress} =require('../../util/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab:1,
    etid:"",
    hand:false,
    aaa:"",
    yajin:"",
    list:[],
    jine:"",
    addressid:"",
    memberId:"",
    packageId:"",
    provinceName:"",
    cityName:"",
    countyName:"",
    telNum:"",
    memberName:"",
    detailInfo:"",
    device:{},
    addressList,
    address:{},
    money:"",
    num:1,
    sharersId:0
    
  },
  

  jian:function(){  
    this.setData({
   num:this.data.num-1,
  
    })
    
  
  },
  jia:function(){
    this.setData({
   num:this.data.num+1,
  })
  },
  tap1(e){
    console.log(e)
    this.setData({
      hand:!this.data.hand
    })
  },
  selected(evt){
    console.log(evt)
    let amount = this.data.yajin;
    let price = evt.currentTarget.dataset.jine;
    let total =  price;
    console.log('evt', total)
    this.setData({
      aaa:evt.currentTarget.dataset.index,
       jine:(new Number(total)).toFixed(2),
       packageId:evt.currentTarget.dataset.id  
     })  
    

  },  
  yonghuxieyi(){
    wx.navigateTo({
      url: '/pages/xieyi/xieyi',
    })
  },
  handTap(){
    if(this.data.addressList.length==0){
      wx.showToast({
        title: "请选择收货地址",
        icon:'error'
      })
      this.clickme()
      return;
    }
    if(!this.data.hand){
      wx.showToast({
        title: "请勾选用户协议",
        icon:'error'
      })
      return;
    }
    let par={
      addressId:this.data.addressList[0].id,
      etId:this.data.etid,
      num:1,
      packageId:this.data.packageId,
      memberId:wx.getStorageSync('memberId'),
      sharersId:this.data.sharersId == null ? '' : this.data.sharersId
    }
    affirm(par).then(res=>{
      console.log(res)
      if(res.code == 200)
     {
       let p = {
        id:res.data.id,
        payType:0,
        type:0
       }
      payment(p).then(res=>{
        if(res.code==200){
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.sign,
          success (res) {
            console.log("支付成功 ")
            wx.showModal({
              title:'提示',
              content:'支付成功',
              success (res) {
               if (res.confirm) {
                 wx.navigateBack({
                   delta: 1,
                 })
                }
             }
            })
           },
          fail (res) { 
            console.log("支付失败 ")
            wx.showToast({
              title: "支付失败",
              icon: 'error',
              duration: 2000
            })
          }
        })}else{
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 2000
          })
        }
       })
     }
     else if(res.code == 500 && res.msg == "请先实名认证")
     {
       wx.showModal({
         title:'提示',
         content:'请先实名认证',
         success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/renzheng/renzheng',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
       })
     }
     else
     {
       wx.showToast({
         title: res.msg,
         icon:'none'
       })
     }
     })
  },
  delete(evt){

     this.setData({
      addresid:evt.currentTarget.dataset.id
    })
    
    let p={
      id:this.data.addresid

    }
    deladdress(p).then(res=>{
     
      let p = {
        memberId:wx.getStorageSync('memberId')
      }
      addressList(p).then(res=>{
        
       if(res.code==200){
      this.setData({
        provinceName:this.data.provinceName,
        cityName:this.data.cityName,
        countyName:this.data.countyName,
        memberName:this.data.memberName,
        telNum:this.data.telNum,
        detailInfo:this.data.detailInfo,
        addressList:res.data,
        address:res.data[0]
      })}else{
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
       }
       )
    })
  },
  tapMove() {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  },

  click(){
    
    this.hideModal();
    wx.navigateTo({
      url: '/pages/dizhi/dizhi',
    })
  },
  revise(e){
    this.setData({
      thisdizhi:e.currentTarget.dataset.data
    })
    let dizhi=JSON.stringify(this.data.thisdizhi)
    
    
     wx.navigateTo({
       url: '/pages/revisedizhi/revisedizhi?thisdizhi='+dizhi,
     })

  },
  jump(e){
  this.hideModal()
    
    this.setData({
      address:this.data.addressList[e.currentTarget.dataset.index]
    })
  },
  //点击我显示底部弹出框
clickme:function(){
  addressList().then(res=>{
    

  
  }
  )
  this.showModal();
},
//显示对话框
 showModal: function () {
  // 显示遮罩层
   var animation= wx.createAnimation({
     duration: 200,
     timingFunction: "linear",
     delay: 0
   })
   this.animation = animation
   animation.translateY(300).step()
   this.setData({
     animationData: animation.export(),
     showModalStatus: true
   })
   setTimeout(function () {
     animation.translateY(0).step()
     this.setData({
       animationData: animation.export()
     })
   }.bind(this), 200)
 },
 //隐藏对话框
 hideModal: function () {
   
   // 隐藏遮罩层
   var animation = wx.createAnimation({
     duration: 200,
     timingFunction: "linear",
     delay: 0
   })
   this.animation = animation
   animation.translateY(300).step()
   this.setData({
     animationData: animation.export(),
   })
   setTimeout(function () {
     animation.translateY(0).step()
     this.setData({
       animationData: animation.export(),
       showModalStatus: false
     })
   }.bind(this), 200)
 },
 showtaocan(e){
   this.setData({
     tab:e.currentTarget.dataset.tab
   })
 },

  onLoad(options) {
 
  
    console.log(options)
    this.setData({
      etid:options.id,
      memberId:wx.getStorageSync('memberId')
    })  
    this.setData({
      sharersId:options.sharersId
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let p1 = {
      memberId:this.data.memberId,
      etId : this.data.etid
    }
    var that = this
    rentalEquipment(p1).then(res=>{
      console.log(1111,res)
      let arr=[];
      arr= res.data.aaEquipmentType.etImgs.split(",")
      res.data.aaEquipmentType.arr=arr;
      that.setData({
        device:res.data.aaEquipmentType
      })
     
      if(res.code==200){
        let jine="0";
          if(res.data.aaEquipmentType.isenable==0){
            jine=res.data.packageList[0].actualFee + res.data.aaEquipmentType.etnewprice
          }
          if(res.data.aaEquipmentType.isenable==1){
            jine= res.data.packageList[0].actualFee + res.data.aaEquipmentType.etPrice
          }
         this.setData({
           list:res.data.packageList,
           yajin:res.data.aaEquipmentType.etPrice,
           packageId:res.data.packageList[0].id,
           jine:jine
         })}
     
      else{
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
     })
    let p = {
      memberId:wx.getStorageSync('memberId')
    }
    addressList(p).then(res=>{
      
     if(res.code==200){
    this.setData({
      provinceName:this.data.provinceName,
      cityName:this.data.cityName,
      countyName:this.data.countyName,
      memberName:this.data.memberName,
      telNum:this.data.telNum,
      detailInfo:this.data.detailInfo,
      addressList:res.data,
      address:res.data[0]
    })}else{
      wx.showToast({
        title: res.msg,
        icon: 'error',
        duration: 2000
      })
    }
     }
     )
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxianginfo.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }

  }
})